/*
 * @(#)MaintEmainArchiveRuleForm.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMapping;
import org.mandarax.kernel.Fact;

import com.dcivision.dms.core.EmailRuleEngineHelper;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.rules.RuleOperationConstant;
import com.dcivision.rules.bean.RuleBaseObject;
import com.dcivision.rules.bean.RuleConclusion;
import com.dcivision.rules.bean.RuleCondition;


/**
 *  <p>Class Name: MaintEmainArchiveRuleForm</p>
 *  <p>Decription: This class is for edit and new rule form purpose.</p>
 * 
 * 
 * @author        nancy zhang
 * @company       DCIVision Limited
 * @creation date 27/07/2003
 * @version       $Revision: 1.31 $
 */
  public class MaintEmailArchiveRuleForm extends AbstractActionForm{

    public static final String REVISION = "$Revision: 1.31 $";
  
    // * Criterion Items
    public static final String CRITERION_RECEIPIENT = "getReceipientUserIDStr";
    public static final String CRITERION_SUBJECT = "getSubject";  
    public static final String CRITERION_MESSAGE = "getAttatchmentNum"; 
    public static final String CRITERION_MESSAGE_BODY = "getMessgeContentString";
    public static final String CRITERION_EMAILTO = "getToEmailAddressAndUserIDGroupIDRoleID";
    public static final String CRITERION_EMAILCC = "getCCEmailAddressAndUserIDGroupIDRoleID";
    public static final String CRITERION_EMAILFROM = "getSenderEmailAddressAndUserIDGroupIDRoleID";
  
    // * Action Items
    public static final String ACTION_NOTIFY_ME = "com.dcivision.rules.action.NotifyEmailArchiveAction";
    public static final String ACTION_RUTE_TO = "com.dcivision.rules.action.SaveEmailToDMSAction";
    public static final String ACTION_START_WORKFLOW = "com.dcivision.rules.action.TriggerWorkflowAction";
 
    private String conditionValue = null;
    private String actionValue = null;
    
    
    private String ID = null;
    private String ruleName = null;
    private String ruleType = null;
    private String ruleCategory = null;
    private String ruleOwner = null;
    private String description = null;
    private List PrerequisiteList = null;
    private Fact ActionFact = null;
	  private String detailDescription = null;
	
    private String prerequisiteRelation = null;	
    private List condition = null;
    private List conclusion = null;
    private String conditionStr = null;
    private String conclusionStr = null;

    private int criterionNum = 1;
    private int actionNum = 1;
    private Integer delID = null;
    private String delType = null;
    private String[] criterionList = null;
    private String relation = RuleOperationConstant.CONDITION_AND;
    private boolean applyOther = false;
    private List allWorkflow = null;
    private String[] allUsers = null;
    private String[] locationPath = null;
    private String[] selectUsers = null;
	
    private String ruleOrder = null;
    private String ruleEnable = null;

    private String rootID = null;
    private String parentID = null;
    private String addFlag = null;
    
    public boolean isApplyOther() {
    	return applyOther;
    }
	
    public void setApplyOther(boolean applyOther) {
    	this.applyOther = applyOther;
    }
    
    public void setRelation(String relation){
    	this.relation=relation;
    }
    
    public String getRelation(){
    	return this.relation;  
    }
    
    public void setCriterionList(String[] criterionList){
    	this.criterionList=criterionList;
    }
    
    public String[] getCriterionList(){
    	return this.criterionList;
    }
    
    public void setDelType(String delType){
    	this.delType=delType;
    }
    
    public String getDelType(){
    	return this.delType;  
    }
    
    public void setDelID(Integer delID){
    	this.delID=delID;  
    }
    
    public Integer getDelID(){
    	return delID;  
    }
    
    public void setCriterionNum(int criterionNum){
    	this.criterionNum=criterionNum;
    }
    
    public int getCriterionNum(){
    	return criterionNum;  
    }
    
    public void setActionNum(int actionNum){
    	this.actionNum=actionNum;
    }
    
    public int getActionNum(){
    	return actionNum;  
    }
    
    public String[] getAllUsers(){
    	return this.allUsers;  
    }
    
    public void setAllUsers(String[] allUsers){
    	this.allUsers=allUsers;  
    }     
    
    public String[] getSelectUsers() {
		return selectUsers;
	}

	public void setSelectUsers(String[] selectUsers) {
		this.selectUsers = selectUsers;
	}

	public String getDescription() {
    	return description;
    }
    
    public void setDescription(String description) {
    	this.description = description;
    }
    
    public String getRuleCategory() {
    	return ruleCategory;
    }
    
    public void setRuleCategory(String ruleCategory) {
    	this.ruleCategory = ruleCategory;
    }
    
    public String getRuleName() {
    	return ruleName;
    }
    
    public void setRuleName(String ruleName) {
    	this.ruleName = ruleName;
    }
    
    public String getRuleOwner() {
    	return ruleOwner;
    }
    
    public void setRuleOwner(String ruleOwner) {
    	this.ruleOwner = ruleOwner;
    }
    
    public String getRuleType() {
    	return ruleType;
    }
    
    public void setRuleType(String ruleType) {
    	this.ruleType = ruleType;
    }
    
    public List getConclusion() {
    	return conclusion;
    }
    
    public void setConclusion(List conclusion) {
    	this.conclusion = conclusion;
    }
    
    public String getPrerequisiteRelation() {
    	return prerequisiteRelation;
    }
    
    public void setPrerequisiteRelation(String prerequisiteRelation) {
    	this.prerequisiteRelation = prerequisiteRelation;
    }
    
    public List getCondition() {
    	
    	return condition;
    }
    
//    public void setCondition(List condition) {     
//    	this.condition = condition;      
//    }
    
    public void setCondition(Object condition) {
      if (condition instanceof List ) {
        this.condition = (List)condition;
      } 
//      else {
//        this.condition = new ArrayList();  
//      }
    }
    
    public String getID() {
    	return ID;
    }
    
    public void setID(String id) {
    	ID = id;
    }
    
    public List getAllWorkflow() {
    	return allWorkflow;
    }
    
    public void setAllWorkflow(List allWorkflow) {
    	this.allWorkflow = allWorkflow;
    }
    
    public String[] getLocationPath() {
    	return locationPath;
    }
    
    public void setLocationPath(String[] locationPath) {
    	this.locationPath = locationPath;
    }	
    
    public String getParentID() {
    	return parentID;
    }
    
    public void setParentID(String parentID) {
    	this.parentID = parentID;
    }
    
    public String getRootID() {
    	return rootID;
    }
    
    public void setRootID(String rootID) {
    	this.rootID = rootID;
    }
    
    
    public Fact getActionFact() {
    	return ActionFact;
    }
    
    public void setActionFact(Fact actionFact) {
    	ActionFact = actionFact;
    }
    
    public List getPrerequisiteList() {
    	return PrerequisiteList;
    }
    
    public void setPrerequisiteList(List prerequisiteList) {
    	PrerequisiteList = prerequisiteList;
    }	
    
    public String getAddFlag() {
    	return addFlag;
    }
    
    public void setAddFlag(String addFlag) {
    	this.addFlag = addFlag;
    }
    
    public String getRuleEnable() {
    	return ruleEnable;
    }
    
    public void setRuleEnable(String ruleEnable) {
    	this.ruleEnable = ruleEnable;
    }
    
    public String getRuleOrder() {
    	return ruleOrder;
    }
    
    public void setRuleOrder(String ruleOrder) {
    	this.ruleOrder = ruleOrder;
    }  
    
		public String getDetailDescription() {
			return detailDescription;
		}
    
    public void setDetailDescription(String detailDescription) {
			this.detailDescription = detailDescription;
		}
    
		public String getActionValue() {
			return actionValue;
		}
    
		public void setActionValue(String actionValue) {
			this.actionValue = actionValue;
		}
    
		public String getConditionValue() {
			return conditionValue;
		}
    
		public void setConditionValue(String conditionValue) {
			this.conditionValue = conditionValue;
		}
    
		public String getConclusionStr() {
			return conclusionStr;
		}
    
		public void setConclusionStr(String conclusionStr) {
			this.conclusionStr = conclusionStr;
		}
    
		public String getConditionStr() {
			return conditionStr;
		}
    
		public void setConditionStr(String conditionStr) {
			this.conditionStr = conditionStr;
		}
    
    public void reset(ActionMapping mapping, HttpServletRequest request) {
    	super.reset(mapping, request);
    	this.setID(null);
    	this.setRuleName(null);
    	//  	this.setRuleType(null);
    	this.setRuleCategory(null);
    	this.setDescription(null);
    	this.setPrerequisiteRelation(null);
    	this.setRelation(RuleOperationConstant.CONDITION_AND);
    	this.setApplyOther(false);
    	this.setAddFlag(null);
    	this.setRuleOrder(null);
    	this.setRuleEnable(null);
      this.setDetailDescription(null);
    	
    	this.setCondition(null);
    	this.setConclusion(null);
    	this.setPrerequisiteList(null);
    	this.setActionFact(null);
      this.setConditionValue(null);
      this.setActionValue(null);
      
    }
    
    
    public void setFormData(AbstractBaseObject baseObj) throws ApplicationException {
    	RuleBaseObject tmpRuleBaseObject = (RuleBaseObject) baseObj;
    	this.setID(tmpRuleBaseObject.getRuleID());
    	this.setRuleName(tmpRuleBaseObject.getRuleName());
    	this.setRuleType(tmpRuleBaseObject.getRuleType());
    	this.setRuleCategory(tmpRuleBaseObject.getRuleCategory());
    	this.setDescription(tmpRuleBaseObject.getDescription());  	
    	this.setPrerequisiteRelation(tmpRuleBaseObject.getPrerequisiteRelation());
    	this.setRelation(tmpRuleBaseObject.getConditionRealtion());
    	this.setApplyOther(tmpRuleBaseObject.isApplyOther());
    	this.setRuleOrder( TextUtility.formatNumberWithZeroPrefix(6,new Integer(Integer.parseInt(tmpRuleBaseObject.getRuleSeqNo()) - 1) )  );
    	this.setRuleEnable(tmpRuleBaseObject.getRuleEnable() );

    	//  	this.setCondition(tmpRuleBaseObject.getCondition());
    	//  	this.setConclusion(tmpRuleBaseObject.getConclusion());  
    	this.setConditionConclusion(tmpRuleBaseObject);
    	this.setPrerequisiteList(tmpRuleBaseObject.getPrerequisiteList());
    	this.setActionFact(tmpRuleBaseObject.getActionFact());
      this.setDetailDescription(tmpRuleBaseObject.getDetailDescription());
     
    	
    	this.setRecordStatus(tmpRuleBaseObject.getRecordStatus());
    	this.setUpdateCount(TextUtility.formatIntegerObj(tmpRuleBaseObject.getUpdateCount()));
    	this.setCreatorID(TextUtility.formatIntegerObj(tmpRuleBaseObject.getCreatorID()));
    	this.setCreateDate(formatTimestamp(tmpRuleBaseObject.getCreateDate()));
    	this.setUpdaterID(TextUtility.formatIntegerObj(tmpRuleBaseObject.getUpdaterID()));
    	this.setUpdateDate(formatTimestamp(tmpRuleBaseObject.getUpdateDate()));
    }
    
    public AbstractBaseObject getFormData() throws ApplicationException {
    	RuleBaseObject tmpRuleBaseObject = new RuleBaseObject();
    	tmpRuleBaseObject.setID(TextUtility.parseIntegerObj(this.getID()));
    	tmpRuleBaseObject.setRuleID(this.getID());
    	tmpRuleBaseObject.setRuleName(this.getRuleName());
    	tmpRuleBaseObject.setRuleType(this.getRuleType());
    	tmpRuleBaseObject.setRuleCategory(this.getRuleCategory());
    	tmpRuleBaseObject.setDescription(this.getDescription());  	
    	tmpRuleBaseObject.setPrerequisiteRelation(this.getPrerequisiteRelation());
    	tmpRuleBaseObject.setConditionRealtion(this.getRelation());
    	tmpRuleBaseObject.setApplyOther(this.isApplyOther());
    	tmpRuleBaseObject.setRuleSeqNo( String.valueOf(Integer.parseInt(Utility.isEmpty(this.getRuleOrder())? "0" : this.getRuleOrder()) + 1) );
    	tmpRuleBaseObject.setRuleEnable( Utility.isEmpty(this.getRuleEnable())? "N" : this.getRuleEnable()  );
    	
    	tmpRuleBaseObject.setCondition(this.getCondition());
    	tmpRuleBaseObject.setConclusion(this.getConclusion());
    	tmpRuleBaseObject.setPrerequisiteList(this.getPrerequisiteList());
    	tmpRuleBaseObject.setActionFact(this.getActionFact());
      tmpRuleBaseObject.setDetailDescription(this.getDetailDescription());
    	
    	tmpRuleBaseObject.setRecordStatus(this.getRecordStatus());
    	tmpRuleBaseObject.setUpdateCount(TextUtility.parseIntegerObj(this.getUpdateCount()));
    	tmpRuleBaseObject.setCreatorID(TextUtility.parseIntegerObj(this.getCreatorID()));
    	tmpRuleBaseObject.setCreateDate(parseTimestamp(this.getCreateDate()));
    	tmpRuleBaseObject.setUpdaterID(TextUtility.parseIntegerObj(this.getUpdaterID()));
    	tmpRuleBaseObject.setUpdateDate(parseTimestamp(this.getUpdateDate()));
    	
    	return tmpRuleBaseObject;
    }
    
    /**
     * set the condition & conclusion
     * @param ruleObj
     */
    private void setConditionConclusion(RuleBaseObject ruleObj ){  	
//    	List conditionList = null;
//    	List conclusionList = null;   
      
      List conditionList = new ArrayList();
      List conclusionList = new ArrayList();   
      
      StringBuffer condBuffer = new StringBuffer();
      StringBuffer actionBuffer = new StringBuffer();
      
    	//get the condition list
    	List tmpCondList = ruleObj.getCondition();
//    	log.debug("condition list ###########################" + tmpCondList.size());
    	for (int i = 0; !Utility.isEmpty(tmpCondList) && i < tmpCondList.size(); i++) {
    		if (!(tmpCondList.get(i) instanceof RuleCondition) ) {
    			conditionList = tmpCondList;
    			break;
    		}
    		String[] condArry = new String[3];
    		RuleCondition condition = (RuleCondition)tmpCondList.get(i);
    		condArry[0] = condition.getTerm()[0];
    		condArry[1] = condition.getPredicateName();
    		condArry[2] = condition.getTerm()[1];
    		
        if (i > 0) {
          condBuffer.append(EmailRuleEngineHelper.SPILT_2);
        }
        
        condBuffer.append(condArry[0]);
        condBuffer.append(EmailRuleEngineHelper.SPILT_1);
        condBuffer.append(condArry[1]);
        condBuffer.append(EmailRuleEngineHelper.SPILT_1);
        condBuffer.append(condArry[2]);    
        
    		conditionList.add(condArry);
    		
    		log.debug("PredicateName: " + condition.getPredicateName());
    		log.debug("Term[]: " + condition.getTerm()[0] + ":" + condition.getTerm()[1]);
    		log.debug("Condition: " + condArry[0] + ":" + condArry[1] + ":" + condArry[2]);
    	}
    	
    	//get the conclusion list
    	List tmpConcList = ruleObj.getConclusion();
//    	log.debug("conclusion list ###########################" + tmpConcList.size());
    	for(int i = 0; !Utility.isEmpty(tmpConcList) && i < tmpConcList.size(); i++){
    		if (!(tmpConcList.get(i) instanceof RuleConclusion) ) {
    			conclusionList = tmpConcList;
    			break;
    		}
    		String[] concArry = new String[2];
    		RuleConclusion conclusion = (RuleConclusion)tmpConcList.get(i);
    		concArry[0] = conclusion.getActionClassName();
    		concArry[1] = TextUtility.join(RuleOperationConstant.SPLIT_STRING, conclusion.getParameter()); 
    		conclusionList.add(concArry);
    		
        if (i > 0) {
          actionBuffer.append(EmailRuleEngineHelper.SPILT_2);
        }
        actionBuffer.append(concArry[0]);
        actionBuffer.append(EmailRuleEngineHelper.SPILT_1);
        actionBuffer.append(concArry[1]);        
        
    		log.debug("Class Name: "+conclusion.getActionClassName() );
    		log.debug("Parameter: "+conclusion.getParameter()[0]);
    		log.debug("Conclusion: " + concArry[0] + ":" + concArry[1]);
    	}
    	
    	if ( Utility.isEmpty(this.getAddFlag()) ) {
    		this.setCondition(conditionList);
    		this.setConclusion(conclusionList);
    	}
    	
    	if ( Utility.isEmpty(this.getAddFlag()) && !Utility.isEmpty(this.getCondition()) ) {
    		this.setCriterionNum( this.getCondition().size() );
    	}
    	if (Utility.isEmpty(this.getAddFlag()) && !Utility.isEmpty(this.getConclusion())) {
    		this.setActionNum( this.getConclusion().size() );
    	}
    	this.setAddFlag(null);
      this.setConditionStr(condBuffer.toString());
      this.setConclusionStr(actionBuffer.toString());
    }

  
}
